package tcpEngine;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.util.List;


public class ServerListener implements Runnable
{
	ServerSocket server;
	Connection client;
	List<Connection> connection;

	public ServerListener(ServerSocket server, List<Connection> connection){
		this.server = server;
		this.connection = connection;
	}
	
	@Override
	public void run()
	{
		while(!server.isClosed()){
			try {
				client = new Connection();
				client.socket = server.accept();
				client.socket.setSoTimeout(4000);
				client.output = new ObjectOutputStream(client.socket.getOutputStream());
				client.output.flush();
				client.input  = new ObjectInputStream(client.socket.getInputStream());
				client.userName = (String)client.input.readObject();
				System.out.println("in coming request form: "+client.userName);
				// The list connection is already synchronized
				connection.add(client);
			} catch (IOException e) {
				e.printStackTrace();
			} catch(ClassNotFoundException e){
				
			}
		}
	}
}
